/**
 * 榜单统计
 * @param {*} method 请求方式
 * @param {*} rankName 榜单名{count_uv:观看结果数、count_ad:观看广告、count_followed:关注状态下观看结果}
 * @returns
 */

function request(method = 'GET', rankName = '') {
    let baseUrl = `https://api.scf.moapp.net.cn/release/rank/${getApp().mox.appid}/${rankName}`;
    const year = new Date().getFullYear();
    const month = new Date().getMonth() + 1;
    const day = new Date().getDate();
    const date = `${year}${month > 9 ? month : '0' + month}${day > 9 ? day : '0' + day}`;
    let data = '';
    if (method === 'GET') {
        baseUrl = 'https://api.scf.moapp.net.cn/release/rank/today_score';
        data = { biz_id: getApp().mox.appid, rank_name: rankName, key: getApp().mox.user.openid };
    } else if(method === 'POST'){
        data = { rank_type: 'daily', date: date, key: getApp().mox.user.openid };
    } else {
        data = { rank_type: 'daily', date: date, key: getApp().mox.user.openid };
    }
    return new Promise((resolve, reject) => {
        wx.request({
            url: baseUrl,
            data: data,
            timeout: 10000,
            method: method,
            header: {
                'content-type': 'application/json'
            },
            success: (res) => {
                if (res.data.code == 'OK' || res.data.code == 'NO_EXIST') {
                    resolve(res.data.data);
                } else {
                    reject({ errMsg: res.data.msg || '未知错误' });
                }
            },
            fail: (err) => {
                reject(err);
            }
        });
    });
}

module.exports = {
    request
};
